<template>
  <div class="cascad-menu" ref="cascadMenu">
    <!--左边菜单-->
    <scroll
      class="left-menu"
      :data="menus"
      ref="leftMenu">
      <div class="left-menu-container">
        <ul>
          <li
            class="left-item"
            ref="leftItem"
            :class="{'current': currentIndex === index}"
            @click="selectLeft(index, $event)"
            v-for="(menu, index) in menus"
            :key="index">
            <p class="text">{{menu.name}}</p>
          </li>
        </ul>
      </div>
    </scroll>
    <!--右边菜单对应内容-->
    <scroll
      class="right-menu"
      :data="menus"
      ref="rightMenu"
      @scroll="scrollHeight"
      :listenScroll="true"
      :probeType="3">
      <div class="right-menu-container">
        <ul>
          <li class="right-item" ref="rightItem" v-for="(menu, i) in menus" :key="i">
            <div class="title">{{menu.name}}</div>
            <ul>
              <li v-for="(item, j) in menu.data" :key="j">
                <div class="data-wrapper">
                  <div class="data">{{item.name}}</div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </scroll>
  </div>
</template>

<script>
  // 子组件（cascad-menu的子组件）
  import Scroll from './scroll'

  export default {
    data() {
      return {
        rightTops: [],
        scrollY: 0,
        leftScrollY: 0
      }
    },
    props: {
      // 数据（需要传入的数据是数组类型）
      menus: {
        required: true,
        type: Array,
        default() {
          return []
        }
      }
    },
    computed: {
      // 选中时的样式标记
      currentIndex() {
        const {scrollY, rightTops} = this
        let index = rightTops.findIndex((height, index) => {
          return scrollY >= rightTops[index] && scrollY < rightTops[index + 1]
        })
        if (scrollY > rightTops[index] + 50) {
          index++;
        }
        return index
      }
    },
    created() {
      this.$nextTick(() => {
        this._calculateHeight()
      })
    },
    methods: {
      // 右边选择事件
      selectLeft(index, event) {
        console.log(index);
        if (!event._constructed) {
          return
        }
        let rightItem = this.$refs.rightItem
        let el = rightItem[index]
        this.$refs.rightMenu.scrollToElement(el, 300)
      },
      scrollHeight(pos) {
        // console.log(pos);
        this.scrollY = Math.abs(Math.round(pos.y))
      },
      // 每一个选项对应的右边内容的感应
      _calculateHeight() {
        let lis = this.$refs.rightItem;
        // console.log(lis)
        let height = 0
        this.rightTops.push(height)
        Array.prototype.slice.call(lis).forEach(li => {
          height += li.clientHeight
          this.rightTops.push(height)
        })
        console.log(this.rightTops)
      }
    },
    components: {
      Scroll
    }
  }
</script>

<style lang="scss" scoped>
  .cascad-menu {
    display: flex;
    position: absolute;
    top: 70px;
    bottom: 100px;
    width: 100%;
    overflow: hidden;
    .left-menu {
      flex: 0 0 80px;
      width: 80px;
      background: #f3f5f7;
      .left-item {
        height: 54px;
        width: 100%;
        /*margin-left: -20px;*/
        &.current {
          width: 200%;
          margin-left: -40px;
          background: #fff;
        }
        .text {
          width: 100%;
          line-height: 54px;
          text-align: center;
        }
      }
    }
    .right-menu {
      flex: 1;
      margin-left: 76px;
      .right-item {
        height: 100%;
        margin-left: -40px;
        border: 1px solid #ccc;
        .title {
          border-bottom: 1px solid #ccc;
          height: 40px;
          line-height: 40px;
          font-size: 18px;
          font-weight: bolder;
        }
        .data-wrapper {
          .data {
            line-height: 40px;
            height: 40px;
           /* margin-left: -40px;*/
          }
        }
      }
    }
  }
</style>
